/*
* @Author: xfy
* @Date:   2017-08-29 10:52:50
* @Last Modified by:   xfy
* @Last Modified time: 2017-08-29 13:59:13
*/
'use strict';
require('./index.css');
require('page/common/nav-simple/index.js');
var _supper = require('util/supper.js');
var _user   = require('service/user-service.js');
//表单里的错误提示
var formError = {
    show : function(errMsg){
        $('.error-item').show().find('.error-msg').text(errMsg);
    },
    hide : function(){
        $('.error-item').hide().find('.error-msg').text('');
    }
};
//page逻辑部分
var page = {
    data : {
        username    : '',
        question    : '',
        answer      : '',
        token       : ''
    },
    init : function(){
        this.onLoad();
        this.bindEvent();
    },
    onLoad : function(){
        this.loadStepUsername();
    },
    bindEvent : function(){
        var _this = this;
        //输入用户名下一步按钮的点击
        $('#submit-username').click(function(){
            _this.operateUsername();
        });
        //输入密保答案的下一步点击
        $('#submit-question').click(function(){
            _this.operateQuestion();
        });
        //输入新密码的下一步点击
        $('#submit-password').click(function(){
            _this.operatePassword();
        });
        //如果按下回车也进行提交
        $('.step-username').keyup(function(e){
            //keyCode === 13表示回车键
            if(e.keyCode === 13){
                _this.operateUsername();
            }
        });
        $('.step-question').keyup(function(e){
            //keyCode === 13表示回车键
            if(e.keyCode === 13){
                _this.operateQuestion();
            }
        });
        $('.step-password').keyup(function(e){
            //keyCode === 13表示回车键
            if(e.keyCode === 13){
                _this.operatePassword();
            }
        });
    },
    //加载输入用户名
    loadStepUsername : function(){
        $('.step-username').show();
        $('#username').focus();
    },
    //加载输入密保问题
    loadStepQuestion : function(){
        //清除错误提示
        formError.hide();
        //容器的切换
        $('.step-username').hide()
            .siblings('.step-question').show()
                .find('.question').text(this.data.question);
        $('#answer').focus();
    },
    //加载输入新密码
    loadStepPassword : function(){
        //清除错误提示
        formError.hide();
        //容器的切换
        $('.step-question').hide()
            .siblings('.step-password').show();
        $('#password').focus();
    },
    //操作用户名
    operateUsername : function(){
        var username = $('#username').val();
        //用户名存在
        if(username){
            _user.getQuestion(username, function(res){
                page.data.username = username;
                page.data.question = res;
                page.loadStepQuestion();
            }, function(errMsg){
                formError.show(errMsg);
            });
        }
        //用户名不存在
        else{
            formError.show('请输入用户名');
        }
    },
    //操作密保问题
    operateQuestion : function(){
        var answer = $('#answer').val();
        //密保答案存在
        if(answer){
            //检查答案
            _user.checkAnswer({
                username : page.data.username,
                question : page.data.question,
                answer   : answer
            }, function(res){
                page.data.answer   = answer;
                page.data.token    = res;
                page.loadStepPassword();
            }, function(errMsg){
                formError.show(errMsg);
            });
        }
        //用户名不存在
        else{
            formError.show('请输入用户名');
        }
    },
    //操作新密码
    operatePassword : function(){
        var password = $('#password').val();
        //密保答案存在
        if(password && password.length >= 6){
            _user.resetPassword({
                username    : page.data.username,
                forgetToken : page.data.token,
                passwordNew : password
            }, function(res){
                window.location.href = './result.html?type=user-pass-reset&go=user-login';
            }, function(errMsg){
                formError.show(errMsg);
            });
        }
        //密码为空
        else{
            formError.show('密码长度不能小于6位');
        }
    }
    
};
$(function(){
    page.init();
});